package eece261.dChatter;

import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class dChatMessage {
	
	private InetSocketAddress address_;
	private byte[] bytes_ = new byte[310];
    //Current msg being sent
	private String msg_;
	//MSG that current msg replied to
    private String last_msg_;
    //Used to determine if message was conforming to standards
    private boolean good_;
    //Op_codes
    //0= normal message
    //1= Request
    //2= Reply to request
    private int op_code_;
    //UserID to be used in packet formation
    private int user_code_;
    //return UserID
    public int getUserCode()
    {
    	return this.user_code_;
    }
    //ctor main
    public dChatMessage (String msg, String last_msg, int OP_CODE, InetSocketAddress address, int user_num) {
      address_ = address;
      //create byte_array;
      this.op_code_ = OP_CODE;
      String temp_msg = last_msg + msg;
      byte[] bytes_msg = temp_msg.getBytes();
      this.user_code_ = user_num;
      bytes_[0]=(byte)OP_CODE;
      bytes_[1]=(byte)user_code_;
      bytes_[2]=(byte)last_msg.length();
      bytes_[3]=(byte)msg.length();
      bytes_[4]=0;
      for(int ii=0; ii<temp_msg.length();ii++)
      {
    	  bytes_[ii+5] = bytes_msg[ii];
      }
      msg_ = msg;
      last_msg_ = last_msg;
      good_ = true;
    }
    //ctor used at initial setup when there is no LastMSG
    public dChatMessage (String msg, int OP_CODE, InetSocketAddress address, int user_num) {
        address_ = address;
        //create byte_array;
        this.op_code_ = OP_CODE;
        String last_msg = "";
        String temp_msg = last_msg + msg;
        this.user_code_ = user_num;
        byte[] bytes_msg = temp_msg.getBytes();
        bytes_[0]=(byte)OP_CODE;
        bytes_[1]=(byte)user_code_;
        bytes_[2]=(byte)last_msg.length();
        bytes_[3]=(byte)msg.length();
        bytes_[4]=0;
        for(int ii=0; ii<temp_msg.length();ii++)
        {
      	  bytes_[ii+5] = bytes_msg[ii];
        }
        msg_ = msg;
        last_msg_ = last_msg;
        good_ = true;
      }
    //ctor for listener
    //take packet and reads info from it
    public dChatMessage (DatagramPacket pckt) {
      address_ = (InetSocketAddress)pckt.getSocketAddress();
      int tmp1 = pckt.getData().length;
      for(int ii=0;ii<tmp1;ii++)
      {
    	  bytes_[ii] = pckt.getData()[ii];
      }

      op_code_ = bytes_[0];
      
      this.user_code_ = bytes_[1];
      byte last_msg_len = bytes_[2];
      byte msg_len = bytes_[3];
      //byte temp2 = bytes_[4];
      this.last_msg_ = null;
      this.msg_ = null;
      if(op_code_ == 0 || op_code_ == 2 || op_code_ == 1)
      {
    	  for(int ii = 5; ii< (int)last_msg_len+5; ii++)
    	  {
    		  this.last_msg_ = this.last_msg_ + (char)bytes_[ii];
    	  }
    	  int temp = 5 + (int)last_msg_len;
    	  int temp2 = temp + (int)msg_len;
    	  for(int ii = temp; ii<temp2; ii++)
    	  {
    		  this.msg_ = this.msg_ + (char)bytes_[ii];
    	  }
    		  this.good_ = true;
      }
      else
      {
    	  this.good_ = false;
      }
    }
    //returns msg current msg replied too
    public String last_message()
    {
    	return this.last_msg_;
    }
    //returns whether dChatMessage is conforming to standards
    public boolean good () {
      return this.good_;
    }
    //returns current message
    public String message () {

      return msg_;
    }
    //returns Multicast sockets info
    public InetSocketAddress getInetSockAdr()
    {
    	return this.getInetSockAdr();
    }
    //returns dChatMessage Op_code
    public int getOPCODE()
    {
    	return this.op_code_;
    }
    //Returns datagram packet based off of byte array
    public DatagramPacket packet () throws SocketException {
    	DatagramPacket t = new DatagramPacket(bytes_, bytes_.length, address_);
    	t.setData(bytes_);
    	return t;
    }
}
